@using AntDesign.Docs.Services
@using AntDesign.TableModels

Multiple: <Switch @bind-Value="multiple"></Switch>
<br/>
<Select Style="width: 200px;"
        ItemValue="c=>c"
        ItemLabel="c=>c.Name"
        DataSource="data"
        @bind-Open="selectOpen"
        @bind-Values="@selectedRows"
        Mode="@(multiple ? SelectMode.Multiple : SelectMode.Default)"
        DropdownMatchSelectWidth="false"
        EnableSearch
        OnSearch="OnSearch">
    <DropdownRender>
        <Table @ref="table" TItem="Column" DataSource="@data" @bind-SelectedRows="selectedRows" RowKey="x=>x.Name" OnRowClick="OnRowClick" Size="TableSize.Small">
            <ColumnDefinitions Context="ctx">
                <Selection Hidden="@(!multiple)" />
                <PropertyColumn Property="c=>c.Name">
                    <a>@ctx.Name</a>
                </PropertyColumn>
                <PropertyColumn Property="c=>c.Age"/>
                 <PropertyColumn Property="c=>c.Address"/>
            </ColumnDefinitions>
        </Table>
    </DropdownRender>
</Select>

@code
{
    ITable table;
    IEnumerable<Column> selectedRows=[];
    Column[] data;
    bool selectOpen = false;
    bool multiple;

    protected override void OnInitialized()
    {
        data = dataSource;
    }

    void OnSearch(string searchValue)
    {
        data = dataSource.Where(x => x.Name.Contains(searchValue, StringComparison.InvariantCultureIgnoreCase)).ToArray();
    }

    void OnRowClick(RowData<Column> rowData)
    {
        if (multiple)
        {
            selectedRows = selectedRows.Contains(rowData.Data)? selectedRows.Except([rowData.Data]) : selectedRows.Concat([rowData.Data]);
        }
        else
        {
            selectedRows = [rowData.Data];
            selectOpen = false;
        }

    }

    record Column
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public string Address { get; set; }
    }
    
    Column[] dataSource =
    {
        new Column()
        {
            Name = "John Brown",
            Age = 32,
            Address = "New York No. 1 Lake Park",
        },
        new Column()
        {
            Name = "Jim Green",
            Age = 42,
            Address = "London No. 1 Lake Park",
        },
        new Column()
        {
            Name = "Joe Black",
            Age = 32,
            Address = "Sidney No. 1 Lake Park",
        },
        new Column()
        {
            Name = "Disabled User",
            Age = 99,
            Address = "Sidney No. 1 Lake Park",
        }
    };
}
